<?php


namespace app\admin\controller\sertec;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="服务技师列表")
 */
class ServiceTechnician extends AdminController
{

    use \app\admin\traits\Curd;
    protected $sort = [
        'id'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\ServiceTechnician();
        
    }
    
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }


    /**
    * @NodeAnotation(title="添加")
    */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['service_name'])){
                $this->error('技师名称不能为空');
            }
            $data = [
                'service_name'=>$post['service_name'],
                'service_img'=>$post['image'],
                'phone'=>$post['phone'],
                'sex'=>$post['sex'],
                'age'=>$post['age'],
                'royalty_ratio'=>$post['royalty_ratio'],
                'password'=>md5("123456"),
                'create_time'=>time()
            ];
            try {
                $save = $this->model->save($data);
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }

     /**
    * @NodeAnotation(title="编辑")
    */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['service_name'])){
                $this->error('技师名称不能为空');
            }
            $data = [
                'service_name'=>$post['service_name'],
                'service_img'=>$post['image'],
                'phone'=>$post['phone'],
                'sex'=>$post['sex'],
                'age'=>$post['age'],
                'royalty_ratio'=>$post['royalty_ratio'],
            ];
            try {
               $this->model->where('id',$post['id'])->update($data);
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $this->success('保存成功') ;
        }else{
            $post = $this->request->param(); 
            $row = $this->model->where('id',$post['id'])->find();
            $this->assign('row', $row);
        }
        return $this->fetch();
    }

    
    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {
        $row = $this->model->whereIn('id', $id)->select()->toArray();
        empty($row[0]) && $this->error('数据不存在');
        $status = Db::name("order_scanning_code")->where("service_user_id",$row[0]["id"])->where("status",0)->find();
        $is_confirm = Db::name("order_scanning_code")->where("service_user_id",$row[0]["id"])->where("is_confirm",0)->find();
        if(empty($status) || empty($is_confirm) ){
            $this->error('有未完成订单不能删除');
        }
        try {
            $save = $row->delete();
        } catch (\Exception $e) {
            $this->error('删除失败');
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }


    
 
} 